package com.hmsg.health;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import com.hmsg.health.config.WxPayConfig;
import com.hmsg.health.exception.SmsSendException;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.Arrays;

/**
 * @Author cwj on 2019/05/13.
 * @Description TODO
 */
@SpringBootApplication
@EnableConfigurationProperties(WxPayConfig.class)
@MapperScan("com.hmsg.health.dao")
@EnableScheduling
public class Application  { 
    public static void main(String[] args)  {
        SpringApplication.run(Application.class, args);
    }
    @Autowired
    private Environment environment;

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return  new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                String[] activeProfiles = environment.getActiveProfiles();
                //只在测试环境跨域
                if (Arrays.asList(activeProfiles).contains("test")) {
                    registry.addMapping("/**")
                            .allowedOrigins("*")
                            .allowedHeaders("*")
                            .allowedMethods("*").allowCredentials(true);
                }
            }
        };
    }

    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        FastJsonRedisSerializer<Object> jsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
//        ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
//        ParserConfig.getGlobalInstance().addAccept("com.hmsg.operate.bean.bo.");
        redisTemplate.setValueSerializer(jsonRedisSerializer);
        redisTemplate.setHashValueSerializer(jsonRedisSerializer);
        redisTemplate.afterPropertiesSet() ;
        return redisTemplate;
    }

//    @Autowired
//    private SendSmsAliService sendSmsAliService;
//    @Override
//    public void run(String... args) throws Exception {
//        sendSmsAliService.sendYuYInfo("18911326240","套餐****");
//    }
}
